<?php

namespace app\total\controller;

use app\data\model\LiveUserList;
use app\data\model\LiveUserLoginlog;
use think\admin\Controller;

/**
 * 数据报表
 * Class Portal
 * @package app\total\controller
 */
class Portal extends Controller
{
    /**
     * 报表
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function index()
    {
        $now_day = date('Y-m-d');
        $now_month = date('Y-m');
        //用户量
        $this->usersTotal = LiveUserList::mk()->count();
        //过期用户
        $this->usersExpire = LiveUserList::mk()->whereRaw('thali_endtime < NOW()')->count();
        //今日活跃
        $this->dayTotal = LiveUserLoginlog::mk()->whereLike('login_time',"{$now_day}%")->count();
        //当月活跃
        $this->monthTotal = LiveUserLoginlog::mk()->whereLike('login_time',"{$now_month}%")->group('user_id')->count();

        //日活跃用户数
        $this->days = [];
        for ($i = 15; $i >= 0; $i--) {
            $date = date('Y-m-d', strtotime("-{$i}days"));
            $this->days[] = [
                '当天日期' => date('m-d', strtotime("-{$i}days")),
                '用户数' => LiveUserLoginlog::mk()->whereLike('login_time',"{$date}%")->count(),
            ];
        }
        //月活跃用户数
        $this->months = [];
        for ($i = 12; $i >= 0; $i--) {
            if($i == 0){
                $date = date('Y-m');
            }else{
                $date = date('Y-m', strtotime("-{$i}months"));
            }
            $this->months[] = [
                '月份' => $date,
                '用户数' => LiveUserLoginlog::mk()->whereLike('login_time',"{$date}%")->group('user_id')->count(),
            ];
        }
        $this->fetch();
    }
}